<?php

namespace app\tenant\controller;

use think\Controller;
use Util\SysDb;
use think\Db;
class Goodinfo extends Controller
{
    public function getstore()
    {
        $this->db = new SysDb();
        $admin = $this->db->table('goods_store')->where(['status' => 1])->lists();
        if (!$admin) {
            exit(json_encode(['code' => 1, 'message' => 'error', "result" => "无仓库"]));
        } else {
            exit(json_encode(['message' => 'succ', 'result' => $admin]));
        }
    }

    public function goodstypes()
    {
        $this->db = new SysDb();
        $admin = $this->db->table('goods_type')->lists();
        if (!$admin) {
            exit(json_encode(['code' => 1, 'message' => 'error', "result" => "无仓库"]));
        } else {
            exit(json_encode(['message' => 'succ', 'result' => $admin]));
        }
    }
    public function goodlist(){
         // ->where(['store_id' => $storeId])->lists()
         $tenantId = trim(input('param.tenantid'));
         $page = trim(input('param.pageNo'));
         $limit= trim(input('param.pageSize'));
         $name  = trim(input('param.name'));
         $storeId  = trim(input('param.storeId'));
         $types  = trim(input('param.types'));
         $where=array(
            "gi.types" => $types,
            "gi.store_id" => $storeId,
            "gi.name" => $name
        );
        foreach ($where as $key=>$value)
        {
            if(empty($value)){
                unset($where[$key]);
            }
        }
         $tol = (intval($page) - 1) * $limit;
         // $this->db = new SysDb();
         // $admin = $this->db->table('goods_info')->join('as gi left join goods_store as gs on gi.store_id = gs.id left join tenant_goods_price as tgp on gi.id = tgp.gooods_id')->where(['store_id' => $storeId])->lists();
         $admin= Db::table('goods_info')
             ->alias(' gi')
             ->join('goods_store gs', 'gi.store_id = gs.id',"left")
             ->join('tenant_goods_price tgp', 'gi.id = tgp.goods_id',"left")
             ->where(['tgp.tenant_id' => $tenantId])
             ->where($where)
             ->limit($tol, $limit)
             ->field(array(' gi.store_id' =>'storeId','gi.types'=>'types','gi.id'=>"giId",'gi.status'=>"giStatus",'gi.cover','gi.name','gi.sales_num'=>'salesNum','gi.descr','gi.weight','tgp.cost','tgp.id'=>'id','tgp.profit'=>'profit','gi.repertory'=>"repertory"))
             ->select();
        
         if (!$admin) {
             exit(json_encode(['code' => 1, 'message' => 'error', "result" => "无商品"]));
         } else {
            $admins= Db::table('goods_info')
            ->alias(' gi')
            ->join('goods_store gs', 'gi.store_id = gs.id',"left")
            ->join('tenant_goods_price tgp', 'gi.id = tgp.goods_id',"left")
            ->where(['tgp.tenant_id' => $tenantId])
            ->where($where)
            ->field(array(' gi.store_id' =>'storeId','gi.types'=>'types','gi.id'=>"giId",'gi.status'=>"giStatus",'gi.cover','gi.name','gi.sales_num'=>'salesNum','gi.descr','gi.weight','tgp.cost','tgp.id'=>'id','tgp.profit'=>'profit','gi.repertory'=>"repertory"))
            ->select();
             $len=count($admins);
             $list=array();
             array_push($list,$admin);
             array_push($list,$len);
             exit(json_encode(['message' => 'succ', 'result' => $list]));
         }
    }
    
    public function goodupdate(){
        $id = trim(input('param.id'));
        $value= trim(input('param.profit'));
        $this->db = new SysDb();
        $admin = $this->db->table('tenant_goods_price')->where(['id' =>  $id])->item();
        $admins = $this->db->table('goods_info')->where(['id' => $admin['goods_id']])->item();
        (float)$admins['tenant_profit']=(float)$value-(float)$admins['tenant_cost'];
        $admin['cost']=$value;
        $count = $this->db->table('tenant_goods_price')->where(['id' =>  $id])->update($admin);
        if ($count) {
            exit(json_encode(['code' => 1, 'message' => 'succ', "result" => $admin['tenant_profit']]));
        }
    }

}
